package com.springcloud.alibabanacosconfig.mq.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.springcloud.alibabanacosconfig.mq.dto.MailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/10/22 17:21
 * @description
 * @Modify By：
 * @ModifyTime： 2019/10/22
 * @Modify marker：
 **/
@Component
@Slf4j
public class MailListener {
    @Autowired
    ObjectMapper objectMapper;


    //使用@RabbitListener 注解指定目标方法作为消费消息的方法，通过注解参水指定所监听的队列
    //或者Binging.
    @RabbitListener(queues="${mail.queue.name}",containerFactory = "singleListenerContainer")
    public  void consumeMailQueue(@Payload byte[] message) throws IOException {
        MailVo mailVo= objectMapper.readValue(message,MailVo.class);
        log.info("Mail 接收到消息：{}",mailVo.getSender());
    }

}
